package com.loongtech.bi.manager.player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loongtech.bi.entity.player.EntityDau;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;

/**
 * 玩家分析-活跃分析manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class ActivePlayerManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private DauManager dauManager;
	@Autowired
	private NewUserManager newUserManager;
	
	//趋势图
	public List<ActiveTendData> getActiveTendData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<ActiveTendData> result = new ArrayList<ActiveTendData>();
		//map
		Map<String,ActiveTendData> map = new HashMap<String,ActiveTendData>();
		//dau
		List<EntityDau> dauList = dauManager.getDauByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for(EntityDau o : dauList) {
			ActiveTendData ActiveTendData = new ActiveTendData();
			ActiveTendData.setDay(o.getStartday());
			ActiveTendData.setDau(o.getDau());
			//map 存入
			map.put(o.getStartday(), ActiveTendData);
		}
		
		//
		List<Object[]> list = newUserManager.getNewUserByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		for(Object[] o : list) {
			ActiveTendData ActiveTendData = map.get(o[0].toString());
			
			if(ActiveTendData != null) {
				ActiveTendData.setNewUser(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), ActiveTendData);
			}
		}
		
		//遍历存入结果
		for(EntityDau o : dauList) {
			ActiveTendData activeTendData = map.get(o.getStartday());
			
			if(activeTendData != null) {
				activeTendData.setActiveOldUser(activeTendData.getDau() - activeTendData.getNewUser());
				result.add(activeTendData);
			}
		}
		Collections.sort(result, new ActiveTendData().comparatorDate);
		return result;
	}
	
	//平台对比
	public List<ActivePlatFormData> getActivePlatFormData(Date dateStart, Date dateEnd) {
		List<ActivePlatFormData> result = new ArrayList<ActivePlatFormData>();
		//map
		Map<String,ActivePlatFormData> map = new HashMap<String,ActivePlatFormData>();
		//dau
		List<Object[]> dauList = dauManager.getDauGroupByPlatform(dateStart, dateEnd);
		
		for(Object[] o : dauList) {
			ActivePlatFormData ActivePlatFormData = new ActivePlatFormData();
			ActivePlatFormData.setPlatformId(o[0].toString());
			ActivePlatFormData.setDau(Integer.parseInt(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), ActivePlatFormData);
		}
		
		//
		List<Object[]> list = newUserManager.getNewUserGroupByPlatform(dateStart, dateEnd);
		
		for(Object[] o : list) {
			ActivePlatFormData ActivePlatFormData = map.get(o[0].toString());
			
			if(ActivePlatFormData != null) {
				ActivePlatFormData.setNewUser(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), ActivePlatFormData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : dauList) {
			ActivePlatFormData ActivePlatFormData = map.get(o[0].toString());
			
			if(ActivePlatFormData != null) {
				ActivePlatFormData.setActiveOldUser(ActivePlatFormData.getDau() - ActivePlatFormData.getNewUser());
				result.add(ActivePlatFormData);
			}
		}
		
		return result;
	}
	
	//渠道对比
	public List<ActiveChannelData> getActiveChannelData(Date dateStart, Date dateEnd) {
		List<ActiveChannelData> result = new ArrayList<ActiveChannelData>();
		//map
		Map<String,ActiveChannelData> map = new HashMap<String,ActiveChannelData>();
		//dau
		List<Object[]> dauList = dauManager.getDauGroupByChannel(dateStart, dateEnd);
		
		for(Object[] o : dauList) {
			ActiveChannelData ActiveChannelData = new ActiveChannelData();
			ActiveChannelData.setChannelId(o[0].toString());
			ActiveChannelData.setDau(Integer.parseInt(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), ActiveChannelData);
		}
		
		//
		List<Object[]> list = newUserManager.getNewUserGroupByChannel(dateStart, dateEnd);
		
		for(Object[] o : list) {
			ActiveChannelData ActiveChannelData = map.get(o[0].toString());
			
			if(ActiveChannelData != null) {
				ActiveChannelData.setNewUser(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), ActiveChannelData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : dauList) {
			ActiveChannelData ActiveChannelData = map.get(o[0].toString());
			
			if(ActiveChannelData != null) {
				ActiveChannelData.setActiveOldUser(ActiveChannelData.getDau() - ActiveChannelData.getNewUser());
				result.add(ActiveChannelData);
			}
		}
		
		return result;
	}
	
	//区服对比
	public List<ActiveServerData> getActiveServerData(Date dateStart, Date dateEnd) {
		List<ActiveServerData> result = new ArrayList<ActiveServerData>();
		//map
		Map<String,ActiveServerData> map = new HashMap<String,ActiveServerData>();
		//dau
		List<Object[]> dauList = dauManager.getDauGroupByServer(dateStart, dateEnd);
		
		for(Object[] o : dauList) {
			ActiveServerData ActiveServerData = new ActiveServerData();
			ActiveServerData.setServerId(o[0].toString());
			ActiveServerData.setDau(Integer.parseInt(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), ActiveServerData);
		}
		
		//
		List<Object[]> list = newUserManager.getNewUserGroupByServer(dateStart, dateEnd);
		
		for(Object[] o : list) {
			ActiveServerData ActiveServerData = map.get(o[0].toString());
			
			if(ActiveServerData != null) {
				ActiveServerData.setNewUser(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), ActiveServerData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : dauList) {
			ActiveServerData ActiveServerData = map.get(o[0].toString());
			
			if(ActiveServerData != null) {
				ActiveServerData.setActiveOldUser(ActiveServerData.getDau() - ActiveServerData.getNewUser());
				result.add(ActiveServerData);
			}
		}
		
		return result;
	}
	
	//趋势的图表
	public class ActiveTendData {
		private String platformId;
		private String channelId;
		private String serverId;
		@ChartFlow(name="日期")
		private String day; // 日期
		
		@ChartSelect(name = "DAU", key = 1)
		@ChartData1(name = "DAU", key = 1)
		private int dau; //
		@ChartSelect(name = "新用户", key = 2)
		@ChartData1(name = "新用户", key = 2)
		private int newUser; //
		@ChartSelect(name = "老用户", key = 3)
		@ChartData1(name = "老用户", key = 3)
		private int activeOldUser;// acu/puc
		
		// 根据日期排序的比较器
		public final Comparator<ActiveTendData> comparatorDate = new Comparator<ActiveTendData>() {
			public int compare(ActiveTendData s1, ActiveTendData s2) {
				return s2.getDay().compareTo(s1.getDay());
			}
		};
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public int getDau() {
			return dau;
		}
		public void setDau(int dau) {
			this.dau = dau;
		}
		public int getNewUser() {
			return newUser;
		}
		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}
		public int getActiveOldUser() {
			return activeOldUser;
		}
		public void setActiveOldUser(int activeOldUser) {
			this.activeOldUser = activeOldUser;
		}
	}
	
	//平台对比
	public class ActivePlatFormData {
		private String platformId;
		@ChartFlow(name="平台")
		private String platformName;
		private String channelId;
		private String serverId;
		private String day; // 日期
		
		@ChartSelect(name = "dau", key = 1)
		@ChartData1(name = "dau", key = 1)
		private int dau; //
		@ChartSelect(name = "新增活跃账号", key = 2)
		@ChartData1(name = "新增活跃账号", key = 2)
		private int newUser; //
		@ChartSelect(name = "持续活跃老账号", key = 3)
		@ChartData1(name = "持续活跃老账号", key = 3)
		private int activeOldUser;// acu/puc
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
			this.platformName = BiPlatformManager.getChannelNameById(platformId);
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public int getDau() {
			return dau;
		}
		public void setDau(int dau) {
			this.dau = dau;
		}
		public int getNewUser() {
			return newUser;
		}
		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}
		public int getActiveOldUser() {
			return activeOldUser;
		}
		public void setActiveOldUser(int activeOldUser) {
			this.activeOldUser = activeOldUser;
		}
		public String getPlatformName() {
			return platformName;
		}
		public void setPlatformName(String platformName) {
			this.platformName = platformName;
		}
	}
	
	//渠道对比
	public class ActiveChannelData {
		private String platformId;
		private String channelId;
		@ChartFlow(name="渠道")
		private String channelName;
		private String serverId;
		private String day; // 日期
		
		@ChartSelect(name = "dau", key = 1)
		@ChartData1(name = "dau", key = 1)
		private int dau; //
		@ChartSelect(name = "新增活跃账号", key = 2)
		@ChartData1(name = "新增活跃账号", key = 2)
		private int newUser; //
		@ChartSelect(name = "持续活跃老账号", key = 3)
		@ChartData1(name = "持续活跃老账号", key = 3)
		private int activeOldUser;// acu/puc
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
			this.channelName = BiPlatformManager.getChannelNameById(channelId);
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public int getDau() {
			return dau;
		}
		public void setDau(int dau) {
			this.dau = dau;
		}
		public int getNewUser() {
			return newUser;
		}
		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}
		public int getActiveOldUser() {
			return activeOldUser;
		}
		public void setActiveOldUser(int activeOldUser) {
			this.activeOldUser = activeOldUser;
		}
		public String getChannelName() {
			return channelName;
		}
		public void setChannelName(String channelName) {
			this.channelName = channelName;
		}
	}
	
	//区服对比
	public class ActiveServerData {
		private String platformId;
		private String channelId;
		private String serverId;
		@ChartFlow(name="区服")
		private String serverName;
		private String day; // 日期
		
		@ChartSelect(name = "dau", key = 1)
		@ChartData1(name = "dau", key = 1)
		private int dau; //
		@ChartSelect(name = "新增活跃账号", key = 2)
		@ChartData1(name = "新增活跃账号", key = 2)
		private int newUser; //
		@ChartSelect(name = "持续活跃老账号", key = 3)
		@ChartData1(name = "持续活跃老账号", key = 3)
		private int activeOldUser;// acu/puc
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
			this.serverName = BiPlatformManager.getServerNameById(serverId);
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public int getDau() {
			return dau;
		}
		public void setDau(int dau) {
			this.dau = dau;
		}
		public int getNewUser() {
			return newUser;
		}
		public void setNewUser(int newUser) {
			this.newUser = newUser;
		}
		public int getActiveOldUser() {
			return activeOldUser;
		}
		public void setActiveOldUser(int activeOldUser) {
			this.activeOldUser = activeOldUser;
		}
		public String getServerName() {
			return serverName;
		}
		public void setServerName(String serverName) {
			this.serverName = serverName;
		}
	}
}
